'use strict';
import React from "react";
import {ListView, RefreshControl, StyleSheet,View,Text,Image,TouchableOpacity} from "react-native";
import Global from "../Commom/GlobalConst";
import StrategyDetailPage from './StrategyDetailPage';
import FoundAPIService from "../Commom/FoundAPIService";
import DateUtil from "../Commom/DateUtil";
import Toast from '../Utils/toast';
const width = Global.Const.size.width;
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F2F3F5'
    },

});


class StrategyPage extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            dataSource: new ListView.DataSource({
                rowHasChanged: (r1, r2) => r1 !== r2
            }),
            flag: false,
            isRefreshing:false,
            searchText: '',
            strategyList:{},
            record_count: 1,
            page: 1,
        };
        this.HotSearch = this.HotSearch.bind(this);
        this.initData = this.initData.bind(this);
    }

    componentDidMount() {
        this.initData();
    }

    rowClick(row){
        const {navigator} = this.props;
        if (navigator) {
            navigator.push({
                name: 'StrategyDetailPage',
                component: StrategyDetailPage,
                passProps: {
                    ID: row.id
                }
            });
        }
    }
    initData() {
        if (this.state.searchText.length == 0) {
            this.setState({
                isRefreshing: true
            });
            FoundAPIService.raiders_list('',1).then((ret)=> {
                this.setState({
                    flag: true,
                    isRefreshing: false,
                    strategyList: ret.result,
                    record_count: ret.record_count,
                    page: Number(ret.page),
                    dataSource: this.state.dataSource.cloneWithRows(ret.result),
                });
            }).catch((err)=> {

            });
        } else {
            FoundAPIService.raiders_list(this.state.searchText,1).then((ret)=> {
                this.setState({
                    flag: true,
                    isRefreshing: false,
                    strategyList: ret.result,
                    record_count: ret.record_count,
                    page: Number(ret.page),
                    dataSource: this.state.dataSource.cloneWithRows(ret.result),
                });
            }).catch((err)=> {

            });
        }
    }
    HotSearch(searchText) {
        if (searchText.length > 0) {
            this.setState({
                isRefreshing: true,
                searchText: searchText
            });
            FoundAPIService.raiders_list(searchText,1).then((ret)=> {
                this.setState({
                    isRefreshing: false,
                    record_count: ret.record_count,
                    page: Number(ret.page),
                    strategyList: ret.result,
                    dataSource: this.state.dataSource.cloneWithRows(ret.result),
                });
            }).catch((err)=> {
                this.setState({
                    isRefreshing: false,
                    record_count: 1,
                    page:1,
                    strategyList: [],
                    dataSource: this.state.dataSource.cloneWithRows([]),
                });
            });
        } else {
            this.onRefresh();
            this.setState({
                searchText: ''
            });
        }
    }
    onRefresh() {
        if (this.state.searchText.length == 0) {
            this.setState({
                isRefreshing: true
            });
            FoundAPIService.raiders_list('',1).then((ret)=> {
                this.setState({
                    isRefreshing: false,
                    strategyList: ret.result,
                    record_count: ret.record_count,
                    page: Number(ret.page),
                    dataSource: this.state.dataSource.cloneWithRows(ret.result),
                });
                Toast.show('刷新成功');
            }).catch((err)=> {

            });
        } else {
            FoundAPIService.raiders_list(this.state.searchText,1).then((ret)=> {
                this.setState({
                    isRefreshing: false,
                    strategyList: ret.result,
                    record_count: ret.record_count,
                    page: Number(ret.page),
                    dataSource: this.state.dataSource.cloneWithRows(ret.result),
                });
                Toast.show('刷新成功');
            }).catch((err)=> {

            });
        }
    }
    loadMoreData() {

        if (this.state.page < this.state.record_count) {
            this.setState({
                isRefreshing: true,
            });
            if (this.state.searchText.length == 0) {
                FoundAPIService.raiders_list('', this.state.page + 1).then((ret)=> {
                    let wanjian = [];
                    wanjian = this.state.strategyList;
                    for (var i = 0; i < ret.result.length; i++) {
                        wanjian.push(ret.result[i])
                    }
                    this.setState({
                        isRefreshing: false,
                        strategyList: wanjian,
                        record_count: ret.record_count,
                        page: Number(ret.page),
                        dataSource: this.state.dataSource.cloneWithRows(wanjian),
                    });
                    Toast.show('加载成功');
                }).catch((err)=> {

                });
            } else {
                FoundAPIService.raiders_list(this.state.searchText, this.state.page + 1).then((ret)=> {
                    let wanjian = [];
                    wanjian = this.state.strategyList;
                    for (var i = 0; i < ret.result.length; i++) {
                        wanjian.push(ret.result[i])
                    }
                    this.setState({
                        isRefreshing: false,
                        strategyList: wanjian,
                        record_count: ret.record_count,
                        page: Number(ret.page),
                        dataSource: this.state.dataSource.cloneWithRows(wanjian),
                    });
                    Toast.show('加载成功');
                }).catch((err)=> {

                });
            }
        } else {
            if (this.state.page != 1) {
                Toast.show('木有更多数据');
            }
        }
    }
    _renderRow(row, rowID){
        return(
            <TouchableOpacity onPress={() => this.rowClick(row)}>
                <View style={{width:Global.Const.size.width-20,marginLeft:10,height:180,marginBottom:10}}>
                    <Image resizeMode='cover' source={{uri:row.backgroundimage}}
                           style={{width: Global.Const.size.width-20, height: 180,position: 'absolute',left: 0,top: 0,borderRadius:5}}/>
                    <View style={{backgroundColor:'rgba(105,105,105,0.3)',width: Global.Const.size.width-20, height: 180,position: 'absolute',left: 0,top: 0,borderRadius:5}}/>
                    <View style={{width:Global.Const.size.width-20,height:30,marginTop:140,flexDirection:'row',alignItems:'center'}}>
                        <Image resizeMode='cover' source={{uri:row.avatar}}
                               style={{width: 30, height: 30,marginLeft:10,borderRadius:15}}/>
                        <Text numberOfLines={1} style={{marginLeft:5,color:'white',fontSize:16,width:Global.Const.size.width-60,backgroundColor: 'rgba(0,0,0,0)'}}>{row.title}</Text>
                    </View>
                </View>
            </TouchableOpacity>
        )
    }

    render() {
        return (
        <View style={styles.container}>
            <ListView
                style={{flex: 1,marginTop:10}}
                dataSource={this.state.dataSource}
                renderRow={(row, rowID) => this._renderRow(row, rowID)}
                onEndReached={() => this.loadMoreData()}
                onEndReachedThreshold={100}
                enableEmptySections={true}
                initialListSize={6}
                refreshControl={
                    <RefreshControl
                        refreshing={this.state.isRefreshing}
                        onRefresh={() => {this.onRefresh();}}
                        colors={['#4CAF50', '#4CAF50', '#4CAF50', '#4CAF50']}
                    />
                }
            />
        </View>
        )
    }


}
export default StrategyPage;